package interfaces.superWidgets.tabs;

import java.util.HashMap;

import interfaces.GUISource;
import interfaces.superWidgets.InterfaceButton;
import interfaces.superWidgets.StaticContent;

import org.fenggui.StatefullWidget;
import org.fenggui.appearance.LabelAppearance;
import org.fenggui.binding.render.ImageFont;
import org.fenggui.decorator.background.PlainBackground;
import org.fenggui.event.ButtonPressedEvent;
import org.fenggui.event.IButtonPressedListener;
import org.fenggui.util.Color;
import org.fenggui.util.Dimension;
import org.fenggui.util.Point;

public class TabSwitchContent extends StaticContent {
	
	protected AbsTabContent tabContent;
	protected StaticContent activeSwitchContent;
	protected InterfaceButton activeSwitchButton;
	
	protected HashMap<StaticContent, InterfaceButton> contentButtonMap;
	
	private Color normalBGColor, selectedBGColor;
	private Color normalTextColor, selectedTextColor;
	private Color normalHoveredColor;
	
	protected Dimension switchContentSize;
	protected Point switchContentPos;
	
	public TabSwitchContent(int width, int height, ImageFont tabFont) {
		this(width, height, tabFont, 0);
	}
	
	public TabSwitchContent(int width, int height, ImageFont tabFont, int tabWidth) {
		super(width, height);
		
		setBGColors(Color.GRAY, Color.LIGHT_GRAY);
		setTextColors(Color.WHITE, Color.BLACK);
		setHoveredColor(Color.BLUE);
		
		tabContent = createTabContent(tabFont, tabWidth);
		addWidget(tabContent);
		
		contentButtonMap = new HashMap<StaticContent, InterfaceButton>();
		
		layout();
	}
	
	protected AbsTabContent createTabContent(ImageFont tabFont, int tabWidth) {
		int tabButtonHeight = getTabButtonHeight(tabFont);
		if(tabWidth == 0) {
			tabContent = new HorizontalTabContent(width, tabButtonHeight);
			int tabY = height - tabButtonHeight;
			tabContent.setY(tabY);
			switchContentSize = new Dimension(width, tabY);
			switchContentPos = new Point(0, 0);
		} else {
			tabContent = new VerticalTabContent(tabWidth, height, tabButtonHeight);
			switchContentSize = new Dimension(width - tabWidth, height);
			switchContentPos = new Point(tabWidth, 0);
		}
		
		return tabContent;
	}
	
	public void setBGColors(Color normalBGColor, Color selectedBGColor) {
		this.normalBGColor = normalBGColor;
		this.selectedBGColor = selectedBGColor;
		
		getAppearance().removeAll();
		getAppearance().add(new PlainBackground(normalBGColor));
	}
	
	public void setHoveredColor(Color normalHoveredColor) {
		this.normalHoveredColor = normalHoveredColor;
	}
	
	public void setTextColors(Color normalTextColor, Color selectedTextColor) {
		this.normalTextColor = normalTextColor;
		this.selectedTextColor = selectedTextColor;
	}
	
	public void addSwitchContent(InterfaceButton button, final StaticContent content) {
		content.setPosition(switchContentPos);
		button.addButtonPressedListener(new IButtonPressedListener() {
			@Override
			public void buttonPressed(ButtonPressedEvent e) {
				switchContent(content);
			}
		});
		content.getAppearance().removeAll();
		content.getAppearance().add(new PlainBackground(selectedBGColor));
		addTabButton(button);
		
		contentButtonMap.put(content, button);
	}
	
	public Dimension getSwitchContentSize() { return switchContentSize; }
	
	public void addTabButton(InterfaceButton button) {
		tabContent.addTabButton(button);
		setTabButtonColor(button, normalBGColor, normalTextColor, normalHoveredColor);
		layout();
	}
	
	private void setTabButtonColor(InterfaceButton button, Color color, Color textColor, Color hoverColor) {
		LabelAppearance app = button.getAppearance();
		app.removeAll();
		app.clearSpacings();
		
		app.add(StatefullWidget.STATE_DEFAULT, new PlainBackground(color));
		app.add(StatefullWidget.STATE_HOVERED, new PlainBackground(hoverColor));
		
		app.setEnabled(StatefullWidget.STATE_DEFAULT, true);
		app.setEnabled(StatefullWidget.STATE_HOVERED, false);
		
		GUISource.setTextColor(button, textColor);
	}
	
	public void switchContent(StaticContent content) {
		if(activeSwitchContent != null) {
			removeWidget(activeSwitchContent);
			setTabButtonColor(contentButtonMap.get(activeSwitchContent), normalBGColor, normalTextColor, normalHoveredColor);
		}
		
		activeSwitchContent = content;
		addWidget(activeSwitchContent);
		
		activeSwitchButton = contentButtonMap.get(activeSwitchContent);
		setTabButtonColor(activeSwitchButton, selectedBGColor, selectedTextColor, selectedBGColor);
		
		layout();
	}
	
	public static int getTabButtonHeight(ImageFont font) {
		return font.getHeight() / 3 * 4;
	}
	
	public void addSwitchListener(StaticContent content, IButtonPressedListener listener) {
		InterfaceButton button = contentButtonMap.get(content);
		assert(button != null);
		button.addButtonPressedListener(listener);
	}
	
	public StaticContent getActiveContent() { return activeSwitchContent; }
}